package websocket.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 数据返回最外层封装对象
 *
 * @author wangfei
 * @version v1.0
 * @since 2019/7/20 2:52
 */
@ApiModel(value = "数据返回-单行数据")
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class ResultDTO<T> implements Serializable {
    /**
     * 状态值
     */
    @ApiModelProperty(name = "code", value = "状态值:0 – 处理成功，403 - 鉴权失败，500 - 系统错误，503 - 参数错误")
    private Integer code;

    /**
     * 提示
     */
    @ApiModelProperty(name = "message", value = "提示")
    private String message;

    /**
     * 返回数据
     */
    @ApiModelProperty(name = "data", value = "返回数据")
    private T data ;

    public ResultDTO(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private ResultDTO(ResultEnum resCode, T data) {
        this.code = resCode.getCode();
        this.message = resCode.getMessage();
        this.data = data;
    }


    /**
     * 请求成功
     * @param data 数据
     * @param <T>
     * @return
     */
    public static <T> ResultDTO<T> success(T data) {
        return new ResultDTO<T>(ResultEnum.success, data);
    }

    /**
     * 请求失败,系统异常
     * @param <T>
     * @return
     */
    public static <T> ResultDTO<T> systemError() {
        return new ResultDTO<T>(ResultEnum.SYSTEM_ERROR,null);
    }
    /**
     * 请求失败,返回失败参数
     * @param <T>
     * @return
     */
    public static <T> ResultDTO<T> paramErrorData(ResultEnum resultEnum,T data) {
        return new ResultDTO<T>(resultEnum,data);
    }
    /**
     * 请求失败,参数错误
     * @param <T>
     * @return
     */
    public static <T>  ResultDTO<T> paramError() {
        return new ResultDTO<T>(ResultEnum.PARAM_ERROR,null);
    }

    /**
     * 请求失败,自定义异常
     * @param <T>
     * @return
     */
    public static <T>  ResultDTO<T> customError(ResultEnum resultEnum) {
        return new ResultDTO<T>(resultEnum,null);
    }

    /**
     * 请求失败,外部返回的异常
     * @param <T>
     * @return
     */
    public static <T>  ResultDTO<T> externalError(int code, String message) {
        return new ResultDTO<T>(code,message);
    }

    /**
     * @Title: fail
     */
    public static <T> ResultDTO<T> fail(ResultEnum resCode) {
        return new ResultDTO<T>(resCode,null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultDTO{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
